const { allServices } = require('../models/mysqlConfig.js')
const verify = require('../utils/verify.js')

const sql = 'select * from users';

class userService {
    //登录
    async login(ctx) {
        const { username, password } = ctx.request.body
        let _sql = `select * from users where username="${username}";`
        let data = await allServices.qeury(_sql).then(async _res => {
            if (_res.length === 0) {
                console.log('账号不存在');
                return {
                    status: 401,
                    msg: '账号不存在'
                }
            } else if (_res[0].password !== password) {
                return {
                    status: 403,
                    msg: '密码错误'
                }
            } else {
                let _verify = await verify.Token(ctx.request);
                let res_ = {
                    create_time: _res[0].create_time,
                    update_time: _res[0].update_time,
                    username: _res[0].username,
                    nickname: _res[0].nickname,
                    id: _res[0].id,
                    email: _res[0].email,
                    address: _res[0].address,
                    birthday: _res[0].birthday,
                    sex: _res[0].sex,
                    role:_res[0].role
                }
                // console.log(res_);
                // res.setHeader('authorization',_verify)//两个参数都必须为字符串，不可以是变量
                return { res: res_, token: _verify }
            }

        }).catch(res => {
            Promise.reject(res);
        })
        return data
    }
    //注册
    async register(ctx) {
        const { username, password, nickname, sex } = ctx.request.body
        console.log(ctx.request.body);
        console.log(username, password, nickname);
        let _sql = `select username from users where username=${username}`
        let data = await allServices.qeury(_sql).then(async _res => {
            if (_res.length > 0) {
                return {
                    status: 402,
                    msg: '账号已被注册'
                }
            } else {
                let insertSql = `insert into users set ?`
                return await allServices.qeury(insertSql, { create_time: new Date(), update_time: new Date(), username: username, password: password, email: null, sex: sex, nickname: nickname, address: null, birthday: null, role:2 }).then(res => {
                    if (res.insertId) {
                        return {
                            status: 200,
                            msg: '注册成功'
                        }
                    } else return
                })
            }
        })
        return data
    }
    //修改
    async modify(ctx) {
        const { username ,nickname, sex, email, address, birthday } = ctx.request.body
        let newDate = new Date().valueOf()
        let _sql = ` update users set nickname= "${nickname}" ,sex= "${sex}" ,email= "${email}" ,birthday= "${birthday}" ,address= "${address}" ,update_time= "${newDate}" where username= "${username}"`;
        let data = await allServices.qeury(_sql).then(async res => {
            _sql = `select username from users where username=${username}`
            if (res.changedRows === 1) {
                return await allServices.qeury(_sql).then(res => {
                    if(res.length > 0){
                        return {
                            ...res,
                            password:""
                        }
                    }
                })
            }
        })
        return data
    }
    //删除用户
    async delete(ctx) {
        const { password , username } = ctx.request.body;
        let _sql = `delete from users where username="${username}" and password="${password}"`
        let data = await allServices.qeury(_sql).then(res => {
            if(res.affectedRows){
                return {
                    msg:"删除成功",
                    status:200
                }
            }
            return '查无此人!!!'
        })
        return data
    }
    //查询全部的信息
    async allMsg(ctx){
        let data = await allServices.qeury(sql).then(async res => {
            res = JSON.parse(JSON.stringify(res))
            console.log(res);
            for (let i of res) {
                i.password = null;
            }
            return [...res];
        })
        return data
    }
    async test(ctx) {
        
    }
    // 修改密码
    async updateUserPassword(ctx) {
        const { password , username , newPassword} = ctx.request.body
        if(password === newPassword){
            return {
                code:202,
                msg:"两次密码不能一致"
            }
        }
        let _sql = `update users set password = ${newPassword} where username = ${username} and password = ${password};`
        let data = await allServices.qeury(_sql).then(async (res) => {
            console.log(res);
            if(res.affectedRows === 1){
                return {
                    code: 200,
                    msg:"修改成功"
                }
            }else{
                return {
                    code:201,
                    msg:"密码错误"
                }
            }
        })
        return data
    }

}

module.exports = new userService();